/*
 * MinIO Cloud Storage (C) 2016, 2018 MinIO, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import React from "react"
import { connect } from "react-redux"
import web from "../web"
import * as alertActions from "../alert/actions"
import jwtDecode from "jwt-decode"
import classNames from "classnames"
import InputGroup from "./InputGroup"
import { Modal, ModalBody, ModalHeader } from "react-bootstrap"

export class AddAddressModal extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      newAddress: "",
      // currentSecretKeyVisible: false,
      // newSecretKeyVisible: false
    }
  }
  // When its shown, it loads the access key from JWT token
  componentWillMount() {
    const token = jwtDecode(web.GetToken())
    console.log(token)
  }

  // Save the auth params and set them.
  setAuth(e) {
    const { showAlert } = this.props

    // if (this.canSubmitAddr()) {
    //   const currentAccessKey = this.state.currentAccessKey
    //   const currentSecretKey = this.state.currentSecretKey
    //   const newAccessKey = this.state.newAccessKey
    //   const newSecretKey = this.state.newSecretKey
    //   web
    //     .SetAuth({
    //       currentAccessKey,
    //       currentSecretKey,
    //       newAccessKey,
    //       newSecretKey
    //     })
    //     .then(data => {
    //       showAlert({
    //         type: "success",
    //         message: "Credentials updated successfully."
    //       })
    //     })
    //     .catch(err => {
    //       showAlert({
    //         type: "danger",
    //         message: err.message
    //       })
    //     })
    // }
  }

  formNewAddr(e) {
    this.setState({
      newAddress: e.target.value
    })
  }

  canSubmitAddr() {
    return (
      this.state.newAddress.length > 0 
    )
  }

  render() {
    const {hideAddAddress} = this.props

    return (
      <Modal bsSize="md" animation={false} show={true}>
        <ModalHeader>新增源数据服务器配置</ModalHeader>
        <ModalBody className="new-addr-modal">
          <InputGroup
            value={this.state.newAddress}
            onChange={this.formNewAddr.bind(this)}
            id="newAddr"
            label="元数据数据库密码"
            name="newAddr"
            type="text"
            spellCheck="false"
            required="required"
            placeholder="请输入密码并登录……"
          />

        </ModalBody>
        <div className="modal-footer">
          <button
            className={classNames({
              btn: true,
              "btn-success": this.canSubmitAddr()
            })}
            disabled={!this.canSubmitAddr()}
            onClick={this.setAuth.bind(this)}
          >
            确定
          </button>
          <button
            className="btn btn-link"
            onClick={hideAddAddress}
          >
            取消
          </button>
        </div>
      </Modal>
    )
  }
}

// const mapStateToProps = state => {
//   return {
//     serverInfo: state.browser.serverInfo
//   }
// }

const mapDispatchToProps = dispatch => {
  return {
    showAlert: alert => dispatch(alertActions.set(alert))
  }
}

export default connect(
  // mapStateToProps,
  mapDispatchToProps
)(AddAddressModal)
